package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.AliyunCallCenterPojo;
import com.xbongbong.paas.pojo.ItemDataPoJo;
import com.xbongbong.paas.pojo.dto.AliyunBatchUpdateCallRecordDTO;
import com.xbongbong.paas.pojo.dto.AliyunBatchUpdateDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallBatchInsertDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallCenterListDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallCenterModelDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallGetCallListDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallRecordConditionReqDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallcenterRecentlyRecordDTO;
import com.xbongbong.paas.pojo.dto.AliyunListSyncCallRecordDTO;
import com.xbongbong.paas.pojo.dto.AliyunListUnSyncCallRecordDTO;
import com.xbongbong.paas.pojo.dto.ExternalCallRecordDTO;
import com.xbongbong.paas.pojo.dto.ExternalSumCallRecordDTO;
import com.xbongbong.paas.pojo.dto.RepairAliYunCallErrorNumberDTO;
import com.xbongbong.paas.pojo.vo.AliyunCallGetCallListVO;
import com.xbongbong.paas.pojo.dto.UinSumCallRecordDTO;
import com.xbongbong.paas.pojo.dto.UinYunCallRecordDTO;
import com.xbongbong.paas.pojo.dto.YunCallDepAllUserListDTO;
import com.xbongbong.paas.pojo.dto.YunCallPageRecordDTO;
import com.xbongbong.paas.pojo.vo.AliyunCallcenterListVO;
import com.xbongbong.paas.pojo.vo.AliyunCallcenterRecentlyRecordVO;
import com.xbongbong.paas.pojo.vo.ExternalSumCallRecordVO;
import com.xbongbong.paas.pojo.vo.UinSumCallRecordVO;
import com.xbongbong.paas.pojo.vo.YunCallRecordPageVO;
import com.xbongbong.paas.pojo.vo.RepairAliYunCallErrorNumberVO;
import com.xbongbong.paas.service.CallCenterCallLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.RepairAliYunCallService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴洪波
 * @version v1.0
 * @date 21/06/29 09:48
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callLog")
public class AliyunCalcenterCallLogController {

    private  final Logger log =  LoggerFactory.getLogger(this.getClass());

    @Resource
    private CallCenterCallLogService callCenterCallLogService;

    @Resource
    private RepairAliYunCallService repairAliYunCallService;

    /**
     * 内部调用
     * 新增一条阿里云呼通话记录
     * 此本新增只是在数据库先插入记录，具体内容待通话结束后保存
     *
     * @param aliyunCallCenterModelDTO 通话记录数据
     * @param br                       br
     * @return 影响行数
     */
    @RequestMapping(value = "/addAliyunCallCenter", method = RequestMethod.POST)
    public String addAliyunCallCenter(@RequestBody @Valid AliyunCallCenterModelDTO aliyunCallCenterModelDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                Integer insert = callCenterCallLogService.aliyunCallCenterInsert(aliyunCallCenterModelDTO);
                response = new XbbResponse<>(insert);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 分页查询呼叫中心通话记录
     *
     * @param aliyunCallCenterListDTO 请求类
     * @param br br
     * @return 通话记录
     */
    @RequestMapping(value = "/listAliyunCallCenterRecord", method = RequestMethod.POST)
    public String listAliyunCallCenterRecord(@RequestBody @Valid AliyunCallCenterListDTO aliyunCallCenterListDTO, BindingResult br) {
        XbbResponse<AliyunCallcenterListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AliyunCallcenterListVO callCenterListVO = callCenterCallLogService.aliyunCallCenterList(aliyunCallCenterListDTO);
                response = new XbbResponse<>(callCenterListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 保存通话记录
     *
     * @param aliyunCallCenterModelDTO 请求类
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/updateAliyunCallCenter", method = RequestMethod.POST)
    public String updateAliyunCallCenter(@RequestBody @Valid AliyunCallCenterModelDTO aliyunCallCenterModelDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                Integer updateAliyunCallCenter = callCenterCallLogService.updateAliyunCallCenter(aliyunCallCenterModelDTO);
                response = new XbbResponse<>(updateAliyunCallCenter);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 根据电话号查询最近通话记录
     *
     * @param aliyunCallCenterModelDTO 请求类
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/findRecentlyCallRecord", method = RequestMethod.POST)
    public String findRecentlyCallRecord(@RequestBody @Valid AliyunCallcenterRecentlyRecordDTO aliyunCallCenterModelDTO, BindingResult br) {
        XbbResponse<AliyunCallcenterRecentlyRecordVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AliyunCallcenterRecentlyRecordVO callRecord = callCenterCallLogService.findRecentlyCallRecord(aliyunCallCenterModelDTO);
                response = new XbbResponse<>(callRecord);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 根据电话号查询最近通话记录
     *
     * @param conditionReqDTO 请求类
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/condition", method = RequestMethod.POST)
    public String findAliyunCallRecordCondition(@RequestBody @Valid AliyunCallRecordConditionReqDTO conditionReqDTO, BindingResult br) {
        XbbResponse<List<ItemDataPoJo>> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                List<ItemDataPoJo> list = callCenterCallLogService.findAliyunCallRecordCondition(conditionReqDTO);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 查询云呼同步的通话记录
     *
     * @param recordDTO 请求类
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/listSyncCallRecord", method = RequestMethod.POST)
    public XbbResponse<List<AliyunCallCenterPojo>> listSyncCallRecord(@RequestBody @Valid AliyunListSyncCallRecordDTO recordDTO, BindingResult br) {
        XbbResponse<List<AliyunCallCenterPojo>> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                List<AliyunCallCenterPojo> list = callCenterCallLogService.listSyncCallRecord(recordDTO);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return response;
    }

    /**
     * 内部调用
     * 查询云呼同步的通话记录
     *
     * @param recordDTO 请求类
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/listUnsyncCallRecord", method = RequestMethod.POST)
    public XbbResponse<List<AliyunCallCenterPojo>> listUnsyncCallRecord(@RequestBody @Valid AliyunListUnSyncCallRecordDTO recordDTO, BindingResult br) {
        XbbResponse<List<AliyunCallCenterPojo>> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                List<AliyunCallCenterPojo> list = callCenterCallLogService.listUnsyncCallRecord(recordDTO);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return response;
    }

    /**
     * 内部调用
     * 批量跟新阿里云呼叫中心的通话记录
     *
     * @param updateDTO 请求类
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/batchUpdateAliyunCallCenter", method = RequestMethod.POST)
    public XbbResponse<Integer> batchUpdateAliyunCallCenter(@RequestBody @Valid AliyunBatchUpdateDTO updateDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                Integer list = callCenterCallLogService.batchUpdateAliyunCallCenter(updateDTO);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return response;
    }
    /**
     * 内部调用
     * 批量跟新阿里云呼叫中心的通话记录录音
     *
     * @param updateDTO 请求类
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/batchUpdateAliyunCallRecordCenter", method = RequestMethod.POST)
    public XbbResponse<Integer> batchUpdateAliyunCallRecordCenter(@RequestBody @Valid AliyunBatchUpdateCallRecordDTO updateDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                Integer list = callCenterCallLogService.batchUpdateAliyunCallRecordCenter(updateDTO);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return response;
    }

    /**
     * 内部调用
     * 分页查询呼叫中心通话记录
     *
     * @param aliyunCallCenterListDTO 请求类
     * @param br br
     * @return 通话记录
     */
    @RequestMapping(value = "/listAliyunCallCenterWorkbenchRecord", method = RequestMethod.POST)
    public String listAliyunCallCenterWorkbenchRecord(@RequestBody @Valid AliyunCallCenterListDTO aliyunCallCenterListDTO, BindingResult br) {
        XbbResponse<AliyunCallcenterListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AliyunCallcenterListVO callCenterListVO = callCenterCallLogService.aliyunCallCenterWorkbenchList(aliyunCallCenterListDTO);
                response = new XbbResponse<>(callCenterListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 批量新增阿里云呼通话记录
     * 此本新增只是在数据库先插入记录，具体内容待通话结束后保存
     *
     * @param batchInsertDTO 通话记录数据
     * @param br                       br
     * @return 影响行数
     */
    @RequestMapping(value = "/aliyunCallBatchAdd", method = RequestMethod.POST)
    public String aliyunCallBatchAdd(@RequestBody @Valid AliyunCallBatchInsertDTO batchInsertDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                Integer insert = callCenterCallLogService.aliyunCallBatchInsert(batchInsertDTO);
                response = new XbbResponse<>(insert);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 根据部门，人员，和时间范围，查询通话记录
     * @author jiping.yang
     * @param br br
     * @return 中心通话记录
     */
    @RequestMapping(value = "/getCallList", method = RequestMethod.POST)
    public XbbResponse<AliyunCallGetCallListVO> getCallList(@RequestBody @Valid AliyunCallGetCallListDTO listDTO, BindingResult br) {
        XbbResponse<AliyunCallGetCallListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AliyunCallGetCallListVO listVO = callCenterCallLogService.getCallList(listDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return response;
    }

    @RequestMapping(value = "/repairAliYunCallErrorNumber", method = RequestMethod.POST)
    public String listErrorCallId(@RequestBody @Valid RepairAliYunCallErrorNumberDTO callRecordDTO, BindingResult bindingResult) {
        XbbResponse<RepairAliYunCallErrorNumberVO> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RepairAliYunCallErrorNumberVO scriptVO = repairAliYunCallService.repairAliYunCallErrorNumber(callRecordDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 新增一条优音云呼的通话记录
     *
     * @param recordDto 通话记录数据
     * @param br        br
     * @return 影响行数
     */
    @RequestMapping(value = "/addUinYunCallRecord", method = RequestMethod.POST)
    public String addUinYunCallRecord(@RequestBody @Valid UinYunCallRecordDTO recordDto, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                Integer insert = callCenterCallLogService.addUinYunCallRecord(recordDto);
                response = new XbbResponse<>(insert);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 优音分页查询通话记录
     *
     * @param pageDto 分页信息
     * @param br        br
     * @return 影响行数
     */
    @RequestMapping(value = "/yunCallRecordPage", method = RequestMethod.POST)
    public String uinYunCallRecordPage(@RequestBody @Valid YunCallPageRecordDTO pageDto, BindingResult br) {
        XbbResponse<YunCallRecordPageVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                YunCallRecordPageVO pageVO = callCenterCallLogService.yunCallRecordPage(pageDto);
                response = new XbbResponse<>(pageVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 优音分页查询通话记录
     *
     * @param dto 分页信息
     * @param br        br
     * @return 影响行数
     */
    @RequestMapping(value = "/listDepAllUser", method = RequestMethod.POST)
    public String listDepAllUser(@RequestBody @Valid YunCallDepAllUserListDTO dto, BindingResult br) {
        XbbResponse<List<String>> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                List<String> list = callCenterCallLogService.listDepAllUser(dto);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 内部调用
     * 优音统计通话记录信息
     *
     * @param pageDto 分页信息
     * @param br        br
     * @return 影响行数
     */
    @RequestMapping(value = "/uinSumCallRecord", method = RequestMethod.POST)
    public String uinSumCallRecord(@RequestBody @Valid UinSumCallRecordDTO dto, BindingResult br) {
        XbbResponse<UinSumCallRecordVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UinSumCallRecordVO list = callCenterCallLogService.uinSumCallRecord(dto);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 内部调用
     * 新增一条优音云呼的通话记录
     *
     * @param recordDto 通话记录数据
     * @param br        br
     * @return 影响行数
     */
    @RequestMapping(value = "/addExternalCallRecord", method = RequestMethod.POST)
    public String addExternalCallRecord(@RequestBody @Valid ExternalCallRecordDTO recordDto, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                Integer insert = callCenterCallLogService.addExternalCallRecord(recordDto);
                response = new XbbResponse<>(insert);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 内部调用
     * 外接统计通话记录信息
     *
     * @param dto 分页信息
     * @param br        br
     * @return 影响行数
     */
    @RequestMapping(value = "/externalSumCallRecord", method = RequestMethod.POST)
    public String externalSumCallRecord(@RequestBody @Valid ExternalSumCallRecordDTO dto, BindingResult br) {
        XbbResponse<ExternalSumCallRecordVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExternalSumCallRecordVO list = callCenterCallLogService.externalSumCallRecord(dto);
                response = new XbbResponse<>(list);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
